package com.zkx.fssb.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkx.fssb.domain.dto.FlowerDto;
import com.zkx.fssb.domain.po.Bouquet;
import com.zkx.fssb.domain.po.Flower;
import com.zkx.fssb.result.PageQuery;
import com.zkx.fssb.result.Result;
import com.zkx.fssb.service.BouquetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/admin/bouquet")
public class BouquetController {

    @Autowired
    private BouquetService bouquetService;

    /**
     * 管理端 分页查询
     * @param pageQuery
     *
     * @return
     */
    @GetMapping("/page")
    public Result<Page> getPage(PageQuery pageQuery){
        log.info(pageQuery.toString());
        Page page =bouquetService.customPage(pageQuery);



        return Result.success(page);


    }

    @PostMapping
    public Result save(@RequestBody Bouquet bouquet){
        bouquet.setCreateTime(LocalDateTime.now());
        bouquetService.save(bouquet);

        return Result.success("新建鲜花成功");
    }



    @GetMapping("/getById")
    public Result<Bouquet> getById(Integer bouquetId){
        Bouquet bouquet = bouquetService.getById(bouquetId);

        return Result.success(bouquet);

    }


    /**
     * 管理端 bouquet
     *
     * @param bouquet
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Bouquet bouquet){
        bouquetService.customUpdate(bouquet);
        return Result.success("编辑花束成功");
    }

    /**
     * 管理端 删除鲜花
     *
     * @param bouquetId
     * @return
     */
    @DeleteMapping
    public Result delete(Integer bouquetId){
        boolean flag = bouquetService.removeById(bouquetId);
        if (flag){
            return Result.success("删除鲜花成功");
        }
        return Result.error("删除鲜花失败");
    }

}
